package com.myexam.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.myexam.model.Student;
import com.myexam.model.Subject;
import com.myexam.model.SubjectNew;
import com.myexam.service.StudentService;
import com.myexam.service.SubjectService;
import com.opensymphony.xwork2.ActionSupport;
@Controller
public class ShowSubjectAnswerAction extends ActionSupport implements
		RequestAware, SessionAware {

	private static final long serialVersionUID = 8606155174799058767L;
	private Map<String, Object> request;
	private Map<String, Object> session;
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private StudentService studentService;
	public Map<String, Object> getRequest() {
		return request;
	}
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	

	@SuppressWarnings("unchecked")
	public String execute() throws Exception{
		List<SubjectNew> subjects = new ArrayList<>();
		List<Integer> subjectIDs = (List<Integer>) session.get("subjectIDs");
		List<String> studentAnswers =  (List<String>) session.get("answer");
		Subject subject =null;
		
		Student student = (Student)session.get("studentInfo");
		student = studentService.getStudentInfo(student.getStudentID());
		session.put("studentInfo", student);
		int i =0;
		for(int subjectID : subjectIDs){
				SubjectNew subjectNew = new SubjectNew();
				subject = subjectService.showSubjectParricular(subjectID);
				subjectNew.setSubjectAnswer(subject.getSubjectAnswer());
				subjectNew.setSubjectID(subject.getSubjectID());
				subjectNew.setSubjectMyAnswer(studentAnswers.get(i));
				i++;
				subjectNew.setSubjectOptionA(subject.getSubjectOptionA());
				subjectNew.setSubjectOptionB(subject.getSubjectOptionB());
				subjectNew.setSubjectOptionC(subject.getSubjectOptionC());
				subjectNew.setSubjectOptionD(subject.getSubjectOptionD());
				subjectNew.setSubjectParse(subject.getSubjectParse());
				subjectNew.setSubjectTitle(subject.getSubjectTitle());
			subjects.add(subjectNew);
		}
		
	
		request.put("subjects1", subjects);
		return SUCCESS;
	}
	

}
